home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / MNetsrc.hqx / Mac TCP_IP Source v.33 / ax25dump.c < prev    next >
C/C++ Source or Header  |  1989-02-22  |  11KB  |  453 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "ax25.h"
  5. #include "timer.h"
  6. #include "lapb.h"
  7. #include "trace.h"
  8.  
  9. #ifdef MAC
  10.     extern int tcptrcflg;
  11.     extern FILE *trcwndp;
  12. #endif
  13.  
  14. /* Dump an AX.25 packet header */
  15. ax25_dump(bpp,check)
  16. struct mbuf **bpp;
  17. int check;    /* Not used */
  18. {
  19.     char *decode_type();
  20.     char tmp[20];
  21.     char control,pid;
  22.     int16 type,ftype();
  23.     struct ax25 hdr;
  24.     struct ax25_addr *hp;
  25.  
  26. #ifdef MAC
  27.     fprintf(trcwndp,"AX25: ");
  28. #else
  29.     printf("AX25: ");
  30. #endif
  31.     /* Extract the address header */
  32.     if(ntohax25(&hdr,bpp) < 0){
  33.         /* Something wrong with the header */
  34. #ifdef MAC
  35.         fprintf(trcwndp," bad header!\n");
  36. #else
  37.         printf(" bad header!\n");
  38. #endif
  39.         return;
  40.     }
  41. #ifdef MAC
  42.     pax25(tmp,&hdr.source);
  43.     fprintf(trcwndp,"%s",tmp);
  44.     pax25(tmp,&hdr.dest);
  45.     fprintf(trcwndp,"->%s",tmp);
  46.     if(hdr.ndigis > 0){
  47.         fprintf(trcwndp," v");
  48.         for(hp = &hdr.digis[0]; hp < &hdr.digis[hdr.ndigis]; hp++){
  49.             /* Print digi string */
  50.             pax25(tmp,hp);
  51.             fprintf(trcwndp," %s%s",tmp,(hp->ssid & REPEATED) ? "*":"");
  52. #else
  53.     pax25(tmp,&hdr.source);
  54.     printf("%s",tmp);
  55.     pax25(tmp,&hdr.dest);
  56.     printf("->%s",tmp);
  57.     if(hdr.ndigis > 0){
  58.         printf(" v");
  59.         for(hp = &hdr.digis[0]; hp < &hdr.digis[hdr.ndigis]; hp++){
  60.             /* Print digi string */
  61.             pax25(tmp,hp);
  62.             printf(" %s%s",tmp,(hp->ssid & REPEATED) ? "*":"");
  63. #endif
  64.         }
  65.     }
  66.     if(pullup(bpp,&control,1) != 1)
  67.         return;
  68.  
  69. #ifdef MAC
  70.     fputc(' ',trcwndp);
  71.     type = ftype(control);
  72.     fprintf(trcwndp,"%s",decode_type(type));
  73.     /* Dump poll/final bit */
  74.     if(control & PF){
  75.         switch(hdr.cmdrsp){
  76.         case COMMAND:
  77.             fprintf(trcwndp,"(P)");
  78.             break;
  79.         case RESPONSE:
  80.             fprintf(trcwndp,"(F)");
  81.             break;
  82.         default:
  83.             fprintf(trcwndp,"(P/F)");
  84.             break;
  85. #else
  86.     putchar(' ');
  87.     type = ftype(control);
  88.     printf("%s",decode_type(type));
  89.     /* Dump poll/final bit */
  90.     if(control & PF){
  91.         switch(hdr.cmdrsp){
  92.         case COMMAND:
  93.             printf("(P)");
  94.             break;
  95.         case RESPONSE:
  96.             printf("(F)");
  97.             break;
  98.         default:
  99.             printf("(P/F)");
  100.             break;
  101. #endif
  102.         }
  103.     }
  104.     /* Dump sequence numbers */
  105. #ifdef MAC
  106.     if((type & 0x3) != U)    /* I or S frame? */
  107.         fprintf(trcwndp," NR=%d",(control>>5)&7);
  108.     if(type == I || type == UI){    
  109.         if(type == I)
  110.             fprintf(trcwndp," NS=%d",(control>>1)&7);
  111.         /* Decode I field */
  112.         if(pullup(bpp,&pid,1) == 1){    /* Get pid */
  113.             switch(pid & (PID_FIRST | PID_LAST)){
  114.             case PID_FIRST:
  115.                 fprintf(trcwndp," First frag");
  116.                 break;
  117.             case PID_LAST:
  118.                 fprintf(trcwndp," Last frag");
  119.                 break;
  120.             case PID_FIRST|PID_LAST:
  121.                 break;    /* Complete message, say nothing */
  122.             case 0:
  123.                 fprintf(trcwndp," Middle frag");
  124.                 break;
  125.             }
  126.             fprintf(trcwndp," pid=");
  127.             switch(pid & 0x3f){
  128.             case PID_ARP:
  129.                 fprintf(trcwndp,"ARP\n");
  130.                 break;
  131.             case PID_NETROM:
  132.                 fprintf(trcwndp,"NET/ROM\n");
  133.                 break;
  134.             case PID_IP:
  135.                 fprintf(trcwndp,"IP\n");
  136.                 break;
  137.             case PID_NO_L3:
  138.                 fprintf(trcwndp,"Text\n");
  139.                 break;
  140.             default:
  141.                 fprintf(trcwndp,"0x%x\n",pid);
  142. #else
  143.     if((type & 0x3) != U)    /* I or S frame? */
  144.         printf(" NR=%d",(control>>5)&7);
  145.     if(type == I || type == UI){    
  146.         if(type == I)
  147.             printf(" NS=%d",(control>>1)&7);
  148.         /* Decode I field */
  149.         if(pullup(bpp,&pid,1) == 1){    /* Get pid */
  150.             switch(pid & (PID_FIRST | PID_LAST)){
  151.             case PID_FIRST:
  152.                 printf(" First frag");
  153.                 break;
  154.             case PID_LAST:
  155.                 printf(" Last frag");
  156.                 break;
  157.             case PID_FIRST|PID_LAST:
  158.                 break;    /* Complete message, say nothing */
  159.             case 0:
  160.                 printf(" Middle frag");
  161.                 break;
  162.             }
  163.             printf(" pid=");
  164.             switch(pid & 0x3f){
  165.             case PID_ARP:
  166.                 printf("ARP\n");
  167.                 break;
  168.             case PID_NETROM:
  169.                 printf("NET/ROM\n");
  170.                 break;
  171.             case PID_IP:
  172.                 printf("IP\n");
  173.                 break;
  174.             case PID_NO_L3:
  175.                 printf("Text\n");
  176.                 break;
  177.             default:
  178.                 printf("0x%x\n",pid);
  179. #endif
  180.             }
  181.             /* Only decode frames that are the first in a
  182.              * multi-frame sequence
  183.              */
  184.             switch(pid & (PID_PID | PID_FIRST)){
  185.             case PID_ARP | PID_FIRST:
  186.                 arp_dump(bpp);
  187.                 break;
  188.             case PID_IP | PID_FIRST:
  189.                 /* Only checksum complete frames */
  190.                 ip_dump(bpp,pid & PID_LAST);
  191.                 break;
  192.             case PID_NETROM | PID_FIRST:
  193.                 netrom_dump(bpp);
  194.                 break;
  195.             }
  196.         }
  197.     } else if(type == FRMR && pullup(bpp,tmp,3) == 3){
  198. #ifdef MAC
  199.         fprintf(trcwndp,": %s",decode_type(ftype(tmp[0])));
  200.         fprintf(trcwndp," Vr = %d Vs = %d",(tmp[1] >> 5) & MMASK,
  201.             (tmp[1] >> 1) & MMASK);
  202.         if(tmp[2] & W)
  203.             fprintf(trcwndp," Invalid control field");
  204.         if(tmp[2] & X)
  205.             fprintf(trcwndp," Illegal I-field");
  206.         if(tmp[2] & Y)
  207.             fprintf(trcwndp," Too-long I-field");
  208.         if(tmp[2] & Z)
  209.             fprintf(trcwndp," Invalid seq number");
  210.         fprintf(trcwndp,"\n");
  211.     } else
  212.         fprintf(trcwndp,"\n");
  213.  
  214.     fflush(trcwndp);
  215. #else
  216.         printf(": %s",decode_type(ftype(tmp[0])));
  217.         printf(" Vr = %d Vs = %d",(tmp[1] >> 5) & MMASK,
  218.             (tmp[1] >> 1) & MMASK);
  219.         if(tmp[2] & W)
  220.             printf(" Invalid control field");
  221.         if(tmp[2] & X)
  222.             printf(" Illegal I-field");
  223.         if(tmp[2] & Y)
  224.             printf(" Too-long I-field");
  225.         if(tmp[2] & Z)
  226.             printf(" Invalid seq number");
  227.         printf("\n");
  228.     } else
  229.         printf("\n");
  230.  
  231.     fflush(stdout);
  232. #endif
  233. }
  234. /* Display NET/ROM network and transport headers */
  235. static
  236. netrom_dump(bpp)
  237. struct mbuf **bpp;
  238. {
  239.     struct ax25_addr src,dest;
  240.     char x;
  241.     char tmp[16];
  242.     char thdr[5];
  243.     register i;
  244.  
  245.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  246.         return;
  247.     /* See if it is a routing broadcast */
  248.     if(uchar(*(*bpp)->data) == 0xff) {
  249.         pullup(bpp,tmp,1);        /* Signature */
  250.         pullup(bpp,tmp,ALEN);
  251.         tmp[ALEN] = '\0';
  252. #ifdef MAC
  253.         fprintf(trcwndp,"NET/ROM Routing: %s\n",tmp);
  254.         for(i = 0;i < 11;i++) {
  255.             if (pullup(bpp,tmp,AXALEN) < AXALEN)
  256.                 break;
  257.             memcpy(src.call,tmp,ALEN);
  258.             src.ssid = tmp[ALEN];
  259.             pax25(tmp,&src);
  260.             fprintf(trcwndp,"        %12s",tmp);
  261.             pullup(bpp,tmp,ALEN);
  262.             tmp[ALEN] = '\0';
  263.             fprintf(trcwndp,"%8s",tmp);
  264.             pullup(bpp,tmp,AXALEN);
  265.             memcpy(src.call,tmp, ALEN);
  266.             src.ssid = tmp[ALEN];
  267.             pax25(tmp,&src);
  268.             fprintf(trcwndp,"    %12s", tmp);
  269.             pullup(bpp,tmp,1);
  270.             fprintf(trcwndp,"    %3u\n", (unsigned)uchar(tmp[0]));
  271. #else
  272.         printf("NET/ROM Routing: %s\n",tmp);
  273.         for(i = 0;i < 11;i++) {
  274.             if (pullup(bpp,tmp,AXALEN) < AXALEN)
  275.                 break;
  276.             memcpy(src.call,tmp,ALEN);
  277.             src.ssid = tmp[ALEN];
  278.             pax25(tmp,&src);
  279.             printf("        %12s",tmp);
  280.             pullup(bpp,tmp,ALEN);
  281.             tmp[ALEN] = '\0';
  282.             printf("%8s",tmp);
  283.             pullup(bpp,tmp,AXALEN);
  284.             memcpy(src.call,tmp, ALEN);
  285.             src.ssid = tmp[ALEN];
  286.             pax25(tmp,&src);
  287.             printf("    %12s", tmp);
  288.             pullup(bpp,tmp,1);
  289.             printf("    %3u\n", (unsigned)uchar(tmp[0]));
  290. #endif
  291.         }
  292.         return;
  293.     }
  294.     /* Decode network layer */
  295.     pullup(bpp,tmp,AXALEN);
  296.     memcpy(src.call,tmp,ALEN);
  297.     src.ssid = tmp[ALEN];
  298.     pax25(tmp,&src);
  299. #ifdef MAC
  300.     fprintf(trcwndp,"NET/ROM: %s",tmp);
  301.  
  302.     pullup(bpp,tmp,AXALEN);
  303.     memcpy(dest.call,tmp,ALEN);
  304.     dest.ssid = tmp[ALEN];
  305.     pax25(tmp,&dest);
  306.     fprintf(trcwndp,"->%s",tmp);
  307.  
  308.     pullup(bpp,&x,1);
  309.     fprintf(trcwndp," ttl %d\n",uchar(x));
  310. #else
  311.     printf("NET/ROM: %s",tmp);
  312.  
  313.     pullup(bpp,tmp,AXALEN);
  314.     memcpy(dest.call,tmp,ALEN);
  315.     dest.ssid = tmp[ALEN];
  316.     pax25(tmp,&dest);
  317.     printf("->%s",tmp);
  318.  
  319.     pullup(bpp,&x,1);
  320.     printf(" ttl %d\n",uchar(x));
  321. #endif
  322.  
  323.     /* Read first five bytes of "transport" header */
  324.     pullup(bpp,thdr,5);
  325.     switch(thdr[4] & 0xf){
  326.     case 0:    /* network PID extension */
  327.         if (thdr[0] == PID_IP && thdr[1] == PID_IP)
  328.             ip_dump(bpp,1) ;
  329.         else
  330. #ifdef MAC
  331.             fprintf(trcwndp,"         protocol family %x, proto %x",
  332.                     uchar(thdr[0]), uchar(thdr[1])) ;
  333.         break ;
  334.     case 1:    /* Connect request */
  335.         fprintf(trcwndp,"         conn rqst: ckt %d/%d",uchar(thdr[0]),uchar(thdr[1]));
  336.         pullup(bpp,&x,1);
  337.         fprintf(trcwndp," wnd %d",x);
  338.         pullup(bpp,(char *)&src,sizeof(struct ax25_addr));
  339.         pax25(tmp,&src);
  340.         fprintf(trcwndp," %s",tmp);
  341.         pullup(bpp,(char *)&dest,sizeof(struct ax25_addr));
  342.         pax25(tmp,&dest);
  343.         fprintf(trcwndp,"->%s",tmp);
  344.         break;
  345.     case 2:    /* Connect acknowledgement */
  346.         fprintf(trcwndp,"         conn ack: ur ckt %d/%d my ckt %d/%d",
  347.             uchar(thdr[0]), uchar(thdr[1]), uchar(thdr[2]),
  348.             uchar(thdr[3]));
  349.         pullup(bpp,&x,1);
  350.         fprintf(trcwndp," wnd %d",x);
  351.         break;
  352.     case 3:    /* Disconnect request */
  353.         fprintf(trcwndp,"         disc: ckt %d/%d",uchar(thdr[0]),uchar(thdr[1]));
  354.         break;
  355.     case 4:    /* Disconnect acknowledgement */
  356.         fprintf(trcwndp,"         disc ack: ckt %d/%d",uchar(thdr[0]),uchar(thdr[1]));
  357.         break;
  358.     case 5:    /* Information (data) */
  359.         fprintf(trcwndp,"         info: ckt %d/%d",uchar(thdr[0]),uchar(thdr[1]));
  360.         fprintf(trcwndp," txseq %d rxseq %d",uchar(thdr[2]), uchar(thdr[3]));
  361.         break;
  362.     case 6:    /* Information acknowledgement */
  363.         fprintf(trcwndp,"         info ack: ckt %d/%d rxseq %d",
  364.                 uchar(thdr[0]),uchar(thdr[1]),uchar(thdr[3]));
  365.         break;
  366.     default:
  367.         fprintf(trcwndp,"         unknown transport type %d", thdr[4] & 0x0f) ;
  368.         break;
  369.     }
  370.     if(thdr[4] & 0x80)
  371.         fprintf(trcwndp," CHOKE");
  372.     if(thdr[4] & 0x40)
  373.         fprintf(trcwndp," NAK");
  374.     fprintf(trcwndp,"\n");
  375. #else
  376.             printf("         protocol family %x, proto %x",
  377.                     uchar(thdr[0]), uchar(thdr[1])) ;
  378.         break ;
  379.     case 1:    /* Connect request */
  380.         printf("         conn rqst: ckt %d/%d",uchar(thdr[0]),uchar(thdr[1]));
  381.         pullup(bpp,&x,1);
  382.         printf(" wnd %d",x);
  383.         pullup(bpp,(char *)&src,sizeof(struct ax25_addr));
  384.         pax25(tmp,&src);
  385.         printf(" %s",tmp);
  386.         pullup(bpp,(char *)&dest,sizeof(struct ax25_addr));
  387.         pax25(tmp,&dest);
  388.         printf("->%s",tmp);
  389.         break;
  390.     case 2:    /* Connect acknowledgement */
  391.         printf("         conn ack: ur ckt %d/%d my ckt %d/%d",
  392.             uchar(thdr[0]), uchar(thdr[1]), uchar(thdr[2]),
  393.             uchar(thdr[3]));
  394.         pullup(bpp,&x,1);
  395.         printf(" wnd %d",x);
  396.         break;
  397.     case 3:    /* Disconnect request */
  398.         printf("         disc: ckt %d/%d",uchar(thdr[0]),uchar(thdr[1]));
  399.         break;
  400.     case 4:    /* Disconnect acknowledgement */
  401.         printf("         disc ack: ckt %d/%d",uchar(thdr[0]),uchar(thdr[1]));
  402.         break;
  403.     case 5:    /* Information (data) */
  404.         printf("         info: ckt %d/%d",uchar(thdr[0]),uchar(thdr[1]));
  405.         printf(" txseq %d rxseq %d",uchar(thdr[2]), uchar(thdr[3]));
  406.         break;
  407.     case 6:    /* Information acknowledgement */
  408.         printf("         info ack: ckt %d/%d rxseq %d",
  409.                 uchar(thdr[0]),uchar(thdr[1]),uchar(thdr[3]));
  410.         break;
  411.     default:
  412.         printf("         unknown transport type %d", thdr[4] & 0x0f) ;
  413.         break;
  414.     }
  415.     if(thdr[4] & 0x80)
  416.         printf(" CHOKE");
  417.     if(thdr[4] & 0x40)
  418.         printf(" NAK");
  419.     printf("\n");
  420. #endif
  421. }
  422. char *
  423. decode_type(type)
  424. int16 type;
  425. {
  426.     switch(uchar(type)){
  427.     case I:
  428.         return "I";
  429.     case SABM:
  430.         return "SABM";
  431.     case DISC:
  432.         return "DISC";
  433.     case DM:
  434.         return "DM";
  435.     case UA:
  436.         return "UA";
  437.     case RR:
  438.         return "RR";
  439.     case RNR:
  440.         return "RNR";
  441.     case REJ:
  442.         return "REJ";
  443.     case FRMR:
  444.         return "FRMR";
  445.     case UI:
  446.         return "UI";
  447.     default:
  448.         return "[invalid]";
  449.     }
  450. }
  451.  
  452.  
  453.